package com.wy;

import sun.misc.Signal;
import sun.misc.SignalHandler;

public class MyJavaService {
    public static volatile boolean flag = true;
    private void registerSignalHandler() {
        Signal.handle(new Signal("TERM"), new SignalHandler() {
            public void handle(Signal signal) {
                // 执行必要的清理工作
                // 退出程序
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                flag = false;
                System.out.println(1111);
//                System.exit(0);
            }
        });
    }
    public static void test1(){
        new Thread(() -> {
            while (flag) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(11);
            }
            System.out.println(222);
        }).start();
    }



    public static void main(String[] args) {
        test1();
        new MyJavaService().registerSignalHandler();
    }
}
